package com.ruoyi.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.dto.ChargingPileHazardsDto;
import com.ruoyi.web.entity.ChargingPileHazards;
import com.ruoyi.web.entity.HazardManagement;
import com.ruoyi.web.service.IChargingPileHazardsService;
import com.ruoyi.web.service.IHazardManagementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/admin/hazards")
public class ChargingPileHazardsController extends BaseController {

    private final IChargingPileHazardsService chargingPileHazardsService;
    private final IHazardManagementService hazardManagementService;

    public ChargingPileHazardsController(IChargingPileHazardsService chargingPileHazardsServiceImpl, IHazardManagementService hazardManagementService) {
        this.chargingPileHazardsService = chargingPileHazardsServiceImpl;
        this.hazardManagementService = hazardManagementService;
    }

    /**
     *
     * @param hazardManagement 派发
     */
    @PostMapping("/distribution")
    public AjaxResult distribution(@RequestBody HazardManagement hazardManagement) {
        ChargingPileHazards dbChargingPileHazards = chargingPileHazardsService.getById(hazardManagement.getId());
        BeanUtil.copyProperties(dbChargingPileHazards, hazardManagement);
        hazardManagement.setId(IdUtil.getSnowflakeNextId());
        hazardManagement.setProcessingStatus(1);
        hazardManagementService.save(hazardManagement);
        dbChargingPileHazards.setProcessingStatus(1);
        chargingPileHazardsService.updateById(dbChargingPileHazards);
        return success();
    }


    /**
     * 查询隐患管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChargingPileHazards chargingPileHazards) {
        startPage();
        List<ChargingPileHazardsDto> list = chargingPileHazardsService.selectChargingPileHazardsDtoList(chargingPileHazards);
        return getDataTable(list);
    }

    /**
     * 导出隐患管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:export')")
    @Log(title = "隐患管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargingPileHazards chargingPileHazards) {
        List<ChargingPileHazards> list = chargingPileHazardsService.selectChargingPileHazardsList(chargingPileHazards);
        ExcelUtil<ChargingPileHazards> util = new ExcelUtil<ChargingPileHazards>(ChargingPileHazards.class);
        util.exportExcel(response, list, "隐患管理数据");
    }

    /**
     * 获取隐患管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(chargingPileHazardsService.selectChargingPileHazardsById(id));
    }

    /**
     * 新增隐患管理
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:add')")
    @Log(title = "隐患管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargingPileHazards chargingPileHazards) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        chargingPileHazards.setCreator(user.getUserName());
        chargingPileHazards.setId(IdUtil.getSnowflakeNextId());
        return toAjax(chargingPileHazardsService.insertChargingPileHazards(chargingPileHazards));
    }

    /**
     * 修改隐患管理
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:edit')")
    @Log(title = "隐患管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargingPileHazards chargingPileHazards) {
        return toAjax(chargingPileHazardsService.updateChargingPileHazards(chargingPileHazards));
    }

    /**
     * 删除隐患管理
     */
    @PreAuthorize("@ss.hasPermi('admin:hazards:remove')")
    @Log(title = "隐患管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(chargingPileHazardsService.deleteChargingPileHazardsByIds(ids));
    }
}
